package hsintiao.com.xintiaoAPP.service.impl;

import hsintiao.com.xintiaoAPP.entity.Feedback;
import hsintiao.com.xintiaoAPP.entity.HelpFeedback;
import hsintiao.com.xintiaoAPP.entity.HelpFeedbackType;
import hsintiao.com.xintiaoAPP.mapper.HelpMapper;
import hsintiao.com.xintiaoAPP.service.HelpService;
import hsintiao.com.xintiaoAPP.util.AjaxResult;
import hsintiao.com.xintiaoAPP.util.ResponseCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import sun.misc.BASE64Decoder;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.*;

@Service
public class HelpServiceImpl implements HelpService {
    @Autowired
    private HelpMapper helpMapper;

    //查询帮助反馈首页的基本帮助信息
    @Override
    public AjaxResult getTopHelp() {
        try {
            List<HelpFeedback> topHelp = helpMapper.getTopHelp(true);
            if(topHelp != null && topHelp.size()>0){
                return AjaxResult.me().setResultObj(topHelp);
            }else {
                return AjaxResult.me().setSuccess(false).setCode(ResponseCode.FAILURE.getCode())
                        .setMessage("查询失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setCode(ResponseCode.FAILURE.getCode())
                    .setMessage("查询失败");
        }
    }

    //查询帮助反馈全部帮助信息
    @Override
    public AjaxResult getAllHelp() {
        try {
            List<HelpFeedback> allHelp = helpMapper.getAllHelp();
            Map<String, Object> map = new HashMap<>();
            List<HelpFeedbackType> types = helpMapper.getAllHelpType();
            if(allHelp != null && allHelp.size()>0){
                for (HelpFeedbackType type:types) {
                    List<HelpFeedback> feedbacksByType = new ArrayList<>();
                    for(HelpFeedback helpFeedback:allHelp){
                        if(helpFeedback.getTypeID() == type.getId()){
                            feedbacksByType.add(helpFeedback);
                            //allHelp.remove(helpFeedback);
                        }
                    }
                    map.put(type.getName(),feedbacksByType);
                }
                return AjaxResult.me().setResultObj(map);
                //return AjaxResult.me().setResultObj(allHelp);
            }else {
                return AjaxResult.me().setSuccess(false).setCode(ResponseCode.FAILURE.getCode())
                        .setMessage("查询失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setCode(ResponseCode.FAILURE.getCode())
                    .setMessage("查询失败");
        }
    }

    @Override
    public AjaxResult getDetailedHelp(Long id) {
        try {
            HelpFeedback helpFeedback = helpMapper.getDetailedHelp(id);
            return AjaxResult.me().setResultObj(helpFeedback);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setCode(ResponseCode.FAILURE.getCode())
                    .setMessage("查询失败");
        }
    }

    //反馈
    @Override
    @Transactional
    public AjaxResult saveFeedback(Feedback feedback) {
        try {
            helpMapper.saveFeedback(feedback);
            return AjaxResult.me().setMessage("反馈成功！");
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setCode(ResponseCode.FAILURE.getCode())
                    .setMessage("反馈失败，请重试");
        }
    }


}
